<?php

namespace app\common\event\site;


use app\admin\service\AdminGroupService;
use app\admin\service\SiteSiteService;

class UpdateSiteGroupService
{

    /**
     * 站点保存后重新刷新站点权限列表
     * @return void
     */
    public function handle($model)
    {
        if(!$model) return;

        $site_ids = (new SiteSiteService())->getIds(['site_group_id' => $model['id']],'site_id');

        if(empty($site_ids)) return;

        //更新权限
        $data = ['rules' => is_array($model['group_roles']) ? implode(',',$model['group_roles']) : $model['group_roles']];
        $where = [
            ['site_id','in',$site_ids],
            ['pid','=',0]
        ];
        (new AdminGroupService())->update($data,$where);
        (new AdminGroupService())->updateChildRules([['site_id','in',$site_ids],['pid','<>',0]],$data['rules']);

    }



}